package com.jolink.omiis.wechat.token;

import java.util.Observable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jolink.omiis.wechat.util.GsonUtil;

/**
 * 通过观察者被观察者的模式进行Token刷新
 * 
 * @author WangYanqing
 *
 */
@Component
public class ComponentAccessTokenObservable extends Observable {
	private static final Logger logger = LoggerFactory.getLogger(ComponentAccessTokenObservable.class);

	@Autowired
	private ComponentAccessTokenObserver cato;

	public ComponentAccessTokenObservable() {
	}

	/**
	 * 刷新ComponentAccessToken 的动作由被观察者发起
	 * 
	 * @param cvt
	 */
	public void refreshToken(ComponentVerifyTicket cvt) {
		long t1 = System.currentTimeMillis();
		addObserver(cato);
		String ticketJsonStr = GsonUtil.obj2Json(cvt);
		logger.info("开始刷新微信服务平台ComponentAccessToken，ticket：{}。", ticketJsonStr);
		setChanged();
		logger.info("通知Observer执行ComponentAccessToken刷新操作。");
		notifyObservers(cvt);
		long t2 = System.currentTimeMillis();
		logger.info("刷新微信服务平台ComponentAccessToken完成，耗时：{}毫秒。", (t2 - t1));
	}
}
